﻿using Domain.IRepositorys;
using Domain.Services;
using DTO;
using Infrastructure.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Cache
{
    public class TaskDetailsCache
    {
        public IUserRepository userRepository { get; set; }
        public ITaskRepository taskRepository { get; set; }
        public IWalletRepository walletRepository { get; set; }
        public IMemoryCacheService memoryCacheService { get; set; }
        public IMService iMService { get; set; }
        public TaskService taskService { get; set; }
        public TaskDetailsOutput GetTaskDetails(int taskID)
        {
            var cache = memoryCacheService.Get<TaskDetailsOutput>("GetTaskDetailsByCache_" + taskID);
            if (cache != null) return (TaskDetailsOutput)cache.Clone();

            var task = taskRepository.GetByKey(taskID);

            var taskU = userRepository.GetByKey(task.UserID);
            var taskUW = walletRepository.Entities.FirstOrDefault(i => i.UserID == task.UserID);
            TaskDetailsOutput taskDetailsOutput = new TaskDetailsOutput();

            taskDetailsOutput.UserSMRZ = taskU.IsValidRealNameInfo;
            taskDetailsOutput.UserIsCJSR = taskU.TraderLevel == Domain.Types.TraderLevel.超级商家;
            taskDetailsOutput.UserIsBJ = taskUW.MarginBlance > 0;
            taskDetailsOutput.UserPic = taskU.HeadImg ?? "https://cdn.taohuikeji.net/app_1/h5/doge.jpg";
            taskDetailsOutput.UserPhone = taskU.Phone;
            taskDetailsOutput.UserNickName = taskU.NickName;
            taskDetailsOutput.UserIM = iMService.GetIMUser(taskU);
            taskDetailsOutput.BeFinishCount = task.BeFinishCount;
            taskDetailsOutput.Price = task.Price;
            taskDetailsOutput.ID = task.ID;
            taskDetailsOutput.Name = task.Name;
            taskDetailsOutput.RemainCount = task.RemainCount;
            taskDetailsOutput.NeedTaskStepData = true;
            taskDetailsOutput.StepFieldData = task.GetStepDataField();
            taskDetailsOutput.NeedSubField = task.NeedSubField;
            taskDetailsOutput.SubFieldName = task.SubFieldName;

            var data = task.GetDataField();
            if (data.Any(i => i.Name.Contains("任务链接")))
            {
                taskDetailsOutput.NeedTaskLink = true;
                taskDetailsOutput.TaskLink = data.First(i => i.Name.Contains("任务链接")).Text;
            }

            taskDetailsOutput.Tags = new List<string>();

            var category = taskService.GetCategory(task.CategoryID);

            taskDetailsOutput.Tags.Add(category.Title);
            taskDetailsOutput.TaskAuditID = task.TaskAudits.FirstOrDefault(i => !i.IsAdd)?.ID;


            memoryCacheService.Set<TaskDetailsOutput>("GetTaskDetailsByCache_" + taskID, taskDetailsOutput, TimeSpan.FromMinutes(2));

            return (TaskDetailsOutput)taskDetailsOutput.Clone();
        }

        internal void RemoveCache(int taskID)
        {
            memoryCacheService.Del("GetTaskDetailsByCache_" + taskID);
        }
    }
}
